package com.cfcc.deptone.assetbarcodemgr.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.cfcc.deptone.assetbarcodemgr.R;
import com.cfcc.deptone.assetbarcodemgr.constant.AssetConstants;
import com.cfcc.deptone.assetbarcodemgr.entity.Product;
import com.cfcc.deptone.assetbarcodemgr.utils.PandianFileUtils;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

public class AssetConsistActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_asset_consist);

        final EditText etRemark = (EditText) findViewById(R.id.et_remark_consist);
        Button btOk = (Button) findViewById(R.id.btn_asset_consist_ok);

        Intent intent = getIntent();
        final Bundle data = intent.getBundleExtra(AssetConstants.ASSET_RECORD);

        final String checkBtnStatus = intent.getStringExtra(AssetConstants.ASSET_CHECK_BTN_STATUS);

        btOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final Product product = PandianFileUtils.buildProductWithBundle(data);

                //设置备注
                product.setRemarks(etRemark.getText().toString());
                //设置当前时间
                SimpleDateFormat df = new SimpleDateFormat(AssetConstants.DATETIME_FORMAT);
                String strDateTime = df.format(new Date());
                product.setPandianDate(strDateTime);

                //将盘点类型改为盘点为1
                //product.setCheckinStatus(AssetConstants.PANDIAN);

                if(AssetConstants.ASSET_CHECK_BTN_CONSIST.equals(checkBtnStatus)) {
                    //将盘点类型改为盘点为1-已盘点
                    product.setCheckinStatus(AssetConstants.PANDIAN);
                } else {
                    //将盘点类型改为盘点为0-未盘点
                    product.setCheckinStatus(AssetConstants.INIT_PANDIAN);
                }


                //修改盘点状态
                PandianFileUtils.modifyByRandom(product, getFilePath());
                //通知用户盘点成功
                Toast.makeText(AssetConsistActivity.this, AssetConstants.MSG_PANDIAN_SUCCESS, Toast.LENGTH_LONG).show();
                //迁移到主页面
                Intent intentMain = new Intent(AssetConsistActivity.this, MainActivity.class);
                startActivity(intentMain);
            }
        });
    }
    private String filePath;

    public String getFilePath() {
        filePath = AssetConsistActivity.this.getFilesDir().getAbsoluteFile() + File.separator + AssetConstants.PANDIAN_TMPFILE_NAME;
        return filePath;
    }
}
